<div class="box box-primary" id="control">
    <div class="box-header">
        <div class="col-sm-9" style="padding-top:8px;">
            <i class="fa fa-retweet">&nbsp;&nbsp;</i>
            <h1 class="box-title" style="font-weight:bold;" id="id_h1_page_title">菜单别名插件</h1>
        </div>
        <div class="col-sm-1" style="padding-top:2px;">
          <button id="id_btn_reset" class="btn btn-sm btn-danger btn-del"><i class="fa fa-trash"></i><span id="id_btn_span_reset">Reset</span></button>
        </div>
        <div class="col-sm-2" style="font-size:16px;padding-top:8px;">
            <a id="id_a_official_doc" target="_blank" href="https://www.ventoy.net/en/plugin_bootconf_replace.html"><span class="fa fa-link"></span><span id="id_span_official_doc">官网文档</span></a>
        </div>
    </div>
    <legend></legend>
    
    <div class="box-body">
      <div class="nav-tabs-custom">
        <ul class="nav nav-tabs" id="id_tab_conf_replace">
            <li class=""><a href="#tab_0" data-toggle="tab" aria-expanded="false" style="font-weight:bold" >conf_replace</a></li>
            <li class=""><a href="#tab_1" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">conf_replace_legacy</a></li>
            <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">conf_replace_uefi</a></li>
            <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">conf_replace_ia32</a></li>
            <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">conf_replace_aa64</a></li>
            <li class=""><a href="#tab_5" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">conf_replace_mips</a></li>
        </ul>
      </div>

      <table id="id_conf_replace_tbl" class="table table-bordered">
        <thead>
          <tr>
            <th style="width: 2%;">#</th>
            <th id="id_th_conf_path" style="width: 30%;"></th>
            <th id="id_th_status" style="width: 5%;"></th>
            <th id="id_th_conf_org" style="width: 20%;"></th>
            <th id="id_th_conf_new" style="width: 30%;"></th>
            <th id="id_th_status" style="width: 5%;"></th>
            <th id="id_th_operation" style="width: 5%;"></th>
          </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    </div>
</div>
<script type="text/javascript">
    
  function VtoyPageLanguageChange(newlang) {
    VtoyCommonChangeLanguage(newlang);
    $('h1[id=id_h1_page_title]').text(g_vtoy_cur_language.STR_PLUG_CONF_REPLACE);

    $("span[id=id_span_file_exist]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_EXIST);
    });
    $("span[id=id_span_file_nonexist]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_NONEXIST);
    });
    $("span[id=id_span_file_fuzzy]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_FUZZY);
    });

    if (newlang === 'en') {
      $('#id_th_conf_path').text('Absolute Path');
      $('#id_th_conf_org').text('org');
      $('#id_th_conf_new').text('new');
    } else {
      $('#id_th_conf_path').text('绝对路径');
      $('#id_th_conf_org').text('org');
      $('#id_th_conf_new').text('new');
      
    }
  }
  
  function FillConfReplaceTable(data) {
    var addbtn = ventoy_get_xslg_addbtn('ConfReplaceAddBtn');
    var delbtn = ventoy_get_xslg_delbtn('ConfReplaceDelBtn');

    var td1, td2, td3, td4, td5, td6, td7;
    var $tbl = $("#id_conf_replace_tbl tbody");
    $tbl.empty();

    for (var i = 0; i < data.length; i++) {
      var $tr;
      td1 = '<td>' + (i + 1) + '</td>';
      td2 = '<td>' + data[i].path + '</td>';
      td3 = '<td>' + ventoy_get_status_line(0, data[i].valid) + '</td>';
      td4 = '<td>' + data[i].org + '</td>';
      td5 = '<td>' + data[i].new + '</td>';
      td6 = '<td>' + ventoy_get_status_line(0, data[i].new_valid) + '</td>';
      td7 = '<td>' + delbtn + '</td>';

      $tr = $('<tr>' + td1 + td2 + td3 + td4 + td5 + td6 + td7 + '</tr>');

      $tr.data('path', data[i].path);
      $tr.data('index', i);
      $tbl.append($tr);
    }

    $tbl.append('<tr><td></td><td></td><td></td><td></td><td></td><td></td><td>' + addbtn + '</td></tr>');
  }

  function VtoyFillCurrentPageItem(data) {
    FillConfReplaceTable(data);
  }


  function OnClickMultiModeTab() {
    var href = $(this).attr('href');
    var index = parseInt(href.substr(5, 1));

    if (index < 0 || index >= g_vtoy_data_default_index || current_tab_index === index) {
      return;
    }
    
    current_tab_index = index;
    VtoyFillCurrentPageItem(m_data_conf_replace[index]);
  }

  //Main process
  var m_conf_iso_path;
  var m_conf_org_path;
  var m_conf_new_path;
  var m_data_conf_replace;
  var current_tab_index = 0;
  callVtoySync({method : 'get_conf_replace'}, function(data) {
    m_data_conf_replace = data;
  });

  var m_conf_replace_validator = $("#ConfReplaceForm").validate({    
      rules: {           
        IsoPath : {
          required: true,
          utfmaxlen: true           
        },
        OrgPath : {
          required: true,
          utfmaxlen: true,
          start_slash: true       
        },
        NewPath: {
          required: true,
          utfmaxlen: true
        }
      },

      submitHandler: function(form) {
          m_conf_iso_path = $('input:text[id=IsoPath]').val();
          m_conf_org_path = $('input:text[id=OrgPath]').val();
          m_conf_new_path = $('input:text[id=NewPath]').val();

          if ((!m_conf_iso_path) || (!m_conf_org_path) || (!m_conf_new_path))
          {
              return;
          }

          m_conf_iso_path = ventoy_replace_slash(m_conf_iso_path);
          m_conf_new_path = ventoy_replace_slash(m_conf_new_path);

          if (!ventoy_common_check_path(m_conf_iso_path)) {
              Message.error(g_vtoy_cur_language.STR_INVALID_FILE_PATH);
              return;
          }

          if (!ventoy_common_check_path(m_conf_new_path)) {
              Message.error(g_vtoy_cur_language.STR_INVALID_FILE_PATH);
              return;
          }

          callVtoy({
              method : 'check_path2',
              dir1: 0,
              fuzzy1: 1,
              path1: m_conf_iso_path,
              dir2: 0,
              fuzzy2: 0,
              path2: m_conf_new_path
          }, function(retdata) {
              if (retdata.exist1 != 0 && retdata.exist2 != 0) {
                var img = 0;
                var data = {
                  "path": m_conf_iso_path.substr(g_current_dir.length),
                  "valid": retdata.exist1,
                  "org": m_conf_org_path,
                  "new": m_conf_new_path.substr(g_current_dir.length),
                  "new_valid": 1,
                  "img":0
                };

                if (m_conf_org_path.substr(0, 8) === '/loader/') {
                  img = 1;
                  data.img = 1;
                }

                callVtoy({
                  method : 'conf_replace_add',
                  index: current_tab_index,
                  path: data.path,
                  org: data.org,
                  new: data.new,
                  img: img
                }, function(e) {
                  m_data_conf_replace[current_tab_index].push(data);
                  FillConfReplaceTable(m_data_conf_replace[current_tab_index]);
                  Message.success(g_vtoy_cur_language.STR_SAVE_SUCCESS);
                });

                $("#ConfReplaceModal").modal('hide');
              } else if (retdata.exist1 === 0) {
                Message.error(g_vtoy_cur_language.STR_INVALID_FILE_PATH);
              } else {
                Message.error(g_vtoy_cur_language.STR_INVALID_NEW_FILE_PATH);
              }
          });
      }
  });

  $("#id_conf_replace_tbl").on('click', '.ConfReplaceAddBtn', function() {
    if (g_current_language === 'en') {
        $('#ConfReplaceForm #ConfReplaceForm_lang_1').text("ISO Path");
        $('#ConfReplaceForm #ConfReplaceForm_lang_2').text(" OK");
        $('#ConfReplaceForm #ConfReplaceForm_lang_3').text("Cancel");
        $('#ConfReplaceForm #id_note_conf_cn').hide();
        $('#ConfReplaceForm #id_note_conf_en').show();
    } else {
        $('#ConfReplaceForm #ConfReplaceForm_lang_1').text("ISO文件路径");
        $('#ConfReplaceForm #ConfReplaceForm_lang_2').text("确定");
        $('#ConfReplaceForm #ConfReplaceForm_lang_3').text("取消");
        $('#ConfReplaceForm #id_note_conf_en').hide();
        $('#ConfReplaceForm #id_note_conf_cn').show();
    }
    
    $('#ConfReplaceForm #id_span_conf_tip1').each(function() {
      var tip = (g_current_os === 'windows') ? '\\ISO\\Ubuntu-20.04-desktop-amd64.iso' : "/ISO/Ubuntu-20.04-desktop-amd64.iso";
      $(this).text(g_current_dir + tip);
    });

    $('#ConfReplaceForm #id_span_conf_tip2').each(function() {
      var tip = (g_current_os === 'windows') ? '\\ISO\\Ubuntu-*****-desktop-amd64.iso' : "/ISO/Ubuntu-*****-desktop-amd64.iso";
      $(this).text(g_current_dir + tip);
    });

    $('#ConfReplaceForm #id_span_conf_tip3').each(function() {
      var tip = (g_current_os === 'windows') ? '\\ventoy\\grub_new.cfg' : "/ventoy/grub_new.cfg";
      $(this).text(g_current_dir + tip);
    });

    m_conf_replace_validator.resetForm(); 
    $("#ConfReplaceModal").modal();
  });

  $("#id_conf_replace_tbl").on('click', '.ConfReplaceDelBtn', function() {
    var $tr = $(this).closest('tr');
		var path = $tr.data('path');
    var index = $tr.data('index');

    callVtoySync({
        method : 'conf_replace_del',
        index: current_tab_index,
        path: path
    }, function(data) {
      m_data_conf_replace[current_tab_index].splice(index, 1);
      FillConfReplaceTable(m_data_conf_replace[current_tab_index]);
      Message.success(g_vtoy_cur_language.STR_SAVE_SUCCESS);
    });
  });


  $('#id_tab_conf_replace a[href="#tab_0"]').click(OnClickMultiModeTab);
  $('#id_tab_conf_replace a[href="#tab_1"]').click(OnClickMultiModeTab);
  $('#id_tab_conf_replace a[href="#tab_2"]').click(OnClickMultiModeTab);
  $('#id_tab_conf_replace a[href="#tab_3"]').click(OnClickMultiModeTab);
  $('#id_tab_conf_replace a[href="#tab_4"]').click(OnClickMultiModeTab);
  $('#id_tab_conf_replace a[href="#tab_5"]').click(OnClickMultiModeTab);
  function UpdateTabTitleIcon(data) {
    CommonUpdateTabTitleIcon(data.exist_conf_replace, '#id_tab_conf_replace a[href="#tab_', 'conf_replace');
  }
  $('#id_btn_reset').click(function() { 
    Modal.confirm({msg:GetResetTabConfigTipMsg(current_tab_index, 'conf_replace')}).on(function(e) {
        if (e) {            
            callVtoySync({
                method : 'conf_replace_del',
                index: current_tab_index,
                path: g_del_all_path
            }, function(data) {
                
            });
            
            m_data_conf_replace[current_tab_index].length = 0;
            VtoyFillCurrentPageItem(m_data_conf_replace[g_vtoy_data_default_index]);
            Message.success(g_vtoy_cur_language.STR_SAVE_SUCCESS);        
        }
    });
  });
  
  $('#id_tab_conf_replace a[href="#tab_0"]').tab('show');
  VtoyFillCurrentPageItem(m_data_conf_replace[0]);
  VtoyPageLanguageChange(g_current_language);

</script>
